﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Abigail.Ui
{
    public partial class UiEditAccount : Form
    {
        Controller c;
        public UiEditAccount(Controller givenC, Model.User givenUser)
        {
            InitializeComponent();
            this.Show();

            c = givenC;
            Model.User user = givenUser;

            txt_initials.Text = user.Initials;
            txt_name.Text = user.Name;
            txt_email.Text = user.Email;
            txt_phone.Text = user.Phone;
            combox_status.Items.Add("Aktiv");
            combox_status.Items.Add("Inaktiv");
            if(user.Activated)
                combox_status.SelectedIndex = 0;
            else
                combox_status.SelectedIndex = 1;

            // Check if user is editing self
            if (user.Initials == c.LoggedInUser.Initials)
            {
                this.Text = "Min information - " + user.Initials;
                combox_status.Enabled = false;
            }

            //If not
            else
            {
                this.Text = "Redigera användare - " + user.Initials;
                combox_status.Enabled = true;
            }
        }

        private void btn_changePassword_Click(object sender, EventArgs e)
        {
            c.OpenUiEditPassword(txt_initials.Text);
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string initials = txt_initials.Text;
            string name = txt_name.Text;
            string email = txt_email.Text;
            string phone = txt_phone.Text;
            bool activated;
            if (combox_status.SelectedIndex == 0)
                activated = true;
            else
                activated = false;

            c.UpdateUserInfo(initials, name, email,phone, activated, this);
        }
    }
}
